package online.flowerinsnow.minecraft.bukkit.manhunt.command;

import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import online.flowerinsnow.minecraft.bukkit.manhunt.Manhunt;
import online.flowerinsnow.minecraft.bukkit.manhunt.core.PlayerTeam;
import online.flowerinsnow.minecraft.bukkit.manhunt.manager.ITeamManager;

import java.util.ArrayList;
import java.util.List;

import static online.flowerinsnow.minecraft.bukkit.manhunt.util.MessageUtils.parseColor;
import static online.flowerinsnow.minecraft.bukkit.manhunt.util.MessageUtils.sendMustBePlayer;

public class CommandTeamChat implements TabExecutor {
    @Override
    public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @NotNull String label, String[] args) {
        if (!(sender instanceof Player)) {
            sendMustBePlayer(sender);
            return true;
        }
        if (args.length == 0) {
            sender.sendMessage(parseColor("&7[&a?&7]&b用法: &e/teamchat(tc) <消息>..."));
            return true;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < args.length; i++) {
            sb.append(args[i]);
            if (i + 1 < args.length) sb.append(" ");
        }
        Player player = (Player) sender;
        ITeamManager teamManager = Manhunt.getTeamManager();
        PlayerTeam team = teamManager.getPlayerTeam(player);
        Bukkit.getOnlinePlayers().forEach(p -> {
            if (teamManager.getPlayerTeam(p) == team) {
                p.sendMessage(parseColor("&7[&b!&7]&a<团队> " + player.getName() + "&7: &e") + sb.toString());
            }
        });
        return true;
    }

    @Override
    public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command cmd, @NotNull String label, String[] args) {
        return new ArrayList<>();
    }
}
